package com.tanhua.service;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.tanhua.dao.AnalysisByDayMapper;
import com.tanhua.dao.LogMapper;
import com.tanhua.domain.YearList;
import com.tanhua.domain.YearVo;
import org.springframework.http.ResponseEntity;

import com.tanhua.utils.ComputeUtil;
import com.tanhua.vo.AnalysisSummaryVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;

import java.text.ParseException;
import java.util.*;

@Service
public class AnalysisByDayService  {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Autowired
    private LogMapper logMapper;

public ResponseEntity summary() {
            AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();

            Long cumulativeUsers = analysisByDayMapper.queryCumulativeUsers();
            summaryVo.setCumulativeUsers(cumulativeUsers); // 累计用户数

            String todayStr = new DateTime().toString("yyyy-MM-dd");
            Date today = new DateTime(todayStr).toDate();

//        select sum(num_active) from tb_analysis_by_day where record_date<='2021-01-17' and record_date>='2020-12-17'
            Long activePassMonth = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(30).toString());
            summaryVo.setActivePassMonth(activePassMonth);//过去30天活跃用户数

            Long activePassWeek = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(7).toString());
            summaryVo.setActivePassWeek(activePassWeek);//过去7天活跃用户

            Integer newUsersToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0102");
            summaryVo.setNewUsersToday(newUsersToday.longValue());//今日新增用户数量

            Integer newUsersYesToday = logMapper.queryNumRegisteredOrLogin(new DateTime(todayStr).minusDays(1).toString(), "0102");
            BigDecimal bigDecimal = ComputeUtil.computeRate(newUsersToday, newUsersYesToday);
            summaryVo.setNewUsersTodayRate(bigDecimal);//今日新增用户涨跌率

            Integer loginTimesToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0101");
            summaryVo.setLoginTimesToday(loginTimesToday.longValue());//今日登录次数

            Integer loginTimesYesToday = logMapper.queryNumRegisteredOrLogin(new DateTime(todayStr).minusDays(1).toString(), "0101");
            BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, loginTimesYesToday);
            summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);// 今日登录次数涨跌率

            Integer activeUsersToday = logMapper.queryNumActive(todayStr);
            summaryVo.setActiveUsersToday(activeUsersToday.longValue());//今日活跃用户数量

            Integer activeUsersYesToday = logMapper.queryNumActive(new DateTime(todayStr).minusDays(1).toString());
            BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, activeUsersYesToday);
            summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);//今日活跃用户涨跌率

            return ResponseEntity.ok(summaryVo);

        }

    /**
     * 统计新增、活跃用户、次日留存率
     * @param sd  开始时间
     * @param ed  结束时间
     * @param type 统计数据类型
     * @return yearVo 封装好的统计数据(包含今年的thisYear和去年的lastYear,为前端需要数据)
     */
    public ResponseEntity users(Long sd, Long ed, String type) throws ParseException {
        //判断开始与结束时间的时间差
        int l = (int) DateUtil.betweenDay(new Date(sd), new Date(ed), true);
        //创建包含数据今年和去年的数组
        List thisYear =new ArrayList();
        List lastYear =new ArrayList();
        //new 一个封装对象
        YearVo yearVo = new YearVo();
        //判断type属性进行属性判断,101新增
        if (type.equals("101") ){
            //遍历时间差 进行数据的填写
            for (int i = 0; i < l; i++) {
                //获取String类型的开始时间,转化为"yyyy-MM-dd"格式,"i"为增加的天数
                String thisTime = DateUtil.offsetDay(new Date(sd), i).toString("yyyy-MM-dd");
                //通过时间获取当前时间数据
                Integer thisCount = analysisByDayMapper.queryRegisteredPass(thisTime);
                //存放今年的数据
                YearList thisYearList = new YearList();
                thisYearList.setTitle(thisTime);
                thisYearList.setAmount(thisCount);
                //获取一年前的时间
                String lastTime = DateUtil.offset(DateUtil.parseDate(thisTime), DateField.YEAR, -1).toString("yyyy-MM-dd");
                Integer lastCount = analysisByDayMapper.queryRegisteredPass(lastTime);
                //存放去年的数据
                YearList lastYearList = new YearList();
                lastYearList.setTitle(lastTime);
                lastYearList.setAmount(lastCount);
                //将获得的数据进行数组添加
                thisYear.add(thisYearList);
                lastYear.add(lastYearList);

            }

            //判断type属性进行属性判断,102活跃
        }else if (type.equals("102")) {
            for (int i = 0; i < l; i++) {
                //102和101的需求解法基本一样 不再表述
                String thisTime = DateUtil.offsetDay(new Date(sd), i).toString("yyyy-MM-dd");
                Integer thisCount = analysisByDayMapper.queryActiveLPass(thisTime);
                YearList thisYearList = new YearList();
                thisYearList.setTitle(thisTime);
                thisYearList.setAmount(thisCount);
                String lastTime = DateUtil.offset(DateUtil.parseDate(thisTime), DateField.YEAR, -1).toString("yyyy-MM-dd");
                Integer lastCount = analysisByDayMapper.queryActiveLPass(lastTime);
                YearList lastYearList = new YearList();
                lastYearList.setTitle(lastTime);
                lastYearList.setAmount(lastCount);
                thisYear.add(thisYearList);
                lastYear.add(lastYearList);

            }
            //判断type属性进行属性判断,103留存
        }else if (type.equals("103")){
            for (int i = 0; i < l; i++) {
                //解法也基本一样,不过留存需要进行今天和昨天的进行环比,可以调用ComputeUtil这个封装好的工具类进行使用
                ///**ComputeUtil工具类
                //     * 计算环比
                //     * @param current 本期计数
                //     * @param last    上一期计数
                //     * @return 环比
                //     */
                String thisTime = DateUtil.offsetDay(new Date(sd), i).toString("yyyy-MM-dd");
                Integer thisCount = analysisByDayMapper.queryRetention1dPass(thisTime);
                Integer yiTianQian = analysisByDayMapper.queryRetention1dPass(new DateTime(thisTime).minusDays(1).toString());
                BigDecimal bigDecimal = ComputeUtil.computeRate(thisCount, yiTianQian);
                YearList thisYearList = new YearList();
                thisYearList.setTitle(thisTime);
                thisYearList.setAmount(bigDecimal.intValue());
                String lastTime = DateUtil.offset(DateUtil.parseDate(thisTime), DateField.YEAR, -1).toString("yyyy-MM-dd");
                Integer lastCount = analysisByDayMapper.queryRetention1dPass(lastTime);
                Integer yiTianQianYear = analysisByDayMapper.queryRetention1dPass(new DateTime(lastTime).minusDays(1).toString());
                BigDecimal bigDecimal1 = ComputeUtil.computeRate(lastCount, yiTianQianYear);
                YearList lastYearList = new YearList();
                lastYearList.setTitle(lastTime);
                lastYearList.setAmount(bigDecimal1.intValue());
                thisYear.add(thisYearList);
                lastYear.add(lastYearList);

            }
        }
        //将获得的今年和去年的数组设置为封装类属性
        yearVo.setThisYear(thisYear);
        yearVo.setLastYear(lastYear);
        //返回封装类
        return ResponseEntity.ok(yearVo);
    }
}


